using UnityEngine;

public class IdleState : IState
{
    private float timer;        // 计时器

    public IdleState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }

    public override void OnEnter()
    {
        parameter.animator.Play("Idle");
    }

    public override void OnUpdate()
    {
        timer += Time.deltaTime;    // 计时器累加

        // 如果被击中了，转换到受击状态
        if (parameter.getHit)
        {
            manager.TransitionState(StateType.Hit);
        }

        // 如果有目标且目标在追逐范围内，则转换到反应状态
        if (parameter.target != null &&
            parameter.target.position.x >= parameter.chasePoints[0].position.x &&
            parameter.target.position.x <= parameter.chasePoints[1].position.x)
        {
            manager.TransitionState(StateType.React);
        }

        // 如果达到空闲时间上限，则转换到巡逻状态
        if (timer >= parameter.idleTime)
        {
            manager.TransitionState(StateType.Patrol);
        }
    }

    public override void OnFixedUpdate()
    {
        
    }

    public override void OnExit()
    {
        timer = 0;    // 重置计时器
    }
}
